import { Bus, CONN_ACTIVITY, CONN_BIG_AMOUNT, CONN_FLUCTUATION } from "./Bus";
import { IBroadcast } from "./IBroadcast";

export class ActivityBroadcast extends IBroadcast {
    // channelParam: any = {"event":"subscribe","symbol":"all","topic":"on_chain","interval":"S1"};
    channelParam: any[] = [
        {
            event: "subscribe",
            symbol: "all",
            topic: "on_chain",
            interval: "S1"
        },
        { event: "subscribe", group: "FLUCTUATION", topic: "ON_CHAIN" },
        { event: "subscribe", group: "BIG_AMOUNT", topic: "ON_CHAIN" }
    ];

    subscribe(): void {
        this.unSubscribe();
        this.channelParam.forEach(i => {
            this.send(JSON.stringify(i));
        });
    }

    unSubscribe(): void {
        this.channelParam.forEach(i => {
            const obj = { ...i };
            obj.event = "unsubscribe";
            // this.socket.send(JSON.stringify(obj));
            this.send(JSON.stringify(obj));
        });
    }

    handle(jsondata: any): void {
        // console.warn("chain act", jsondata);
        if (!jsondata.data) return;
        // if( jsondata.data.type == "swap" && (jsondata.data.outAmount || jsondata.data.inAmount )){
        //     return;
        // }
        switch (jsondata.group) {
            case "ALL":
                // console.log("ALL 鏈上");
                Bus.emit(CONN_ACTIVITY, jsondata.data);
                break;
            case "FLUCTUATION":
                // console.log("FLUCTUATION 成交");
                Bus.emit(CONN_FLUCTUATION, jsondata.data);
                break;
            case "BIG_AMOUNT":
                // console.log("BIG_AMOUNT 大额转账");
                Bus.emit(CONN_BIG_AMOUNT, jsondata.data);
                break;
        }
    }
}
